//
//  LockScenicAlertView.swift
//  LeTu
//
//  Created by g1 on 2024/3/6.
//

import UIKit

class LockScenicAlertView: UIView {

    private let rootView: UIWindow? = AppThemeManager.keyWindow()
    private var backgroundView: UIView = UIView()
    
    var clickBuyHandle: (()->Void)?
    var clickCheckAadHandle: (()->Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    // MARK: - UIButtonAction
    @objc private func buyAction() {
        guard let handle = clickBuyHandle else { return }
        handle()
        hide()
    }
    @objc private func checkAdAction() {
        guard let handle = clickCheckAadHandle else { return }
        handle()
        hide()
    }
    
    // MARK: - configUI
    func configUI() {
        
        guard let rootView = rootView else { return }
        
        // 防止多个 AlertView 重复显示
        if rootView.subviews.filter({ $0.isKind(of: AppMiddleAlertView.self) }).count > 0 { return }
                
        backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0)
        
        rootView.addSubview(backgroundView)
        rootView.addSubview(self)
        
        backgroundView.snp.makeConstraints { (make) in
            make.edges.equalTo(rootView)
        }
        snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        addSubview(backView)
        backView.addSubview(iconImgView)
        backView.addSubview(titleLabel)
        backView.addSubview(buyBtn)
        backView.addSubview(checkAdBtn)
        
        backView.snp.makeConstraints { make in
            make.width.equalTo(screenFixationWidth-56*2)
            make.center.equalToSuperview()
        }
        iconImgView.snp.makeConstraints { make in
            make.top.equalTo(38)
            make.width.equalTo(48)
            make.height.equalTo(58)
            make.centerX.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(iconImgView.snp.bottom).offset(24)
        }
        buyBtn.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(54)
            make.width.equalTo(232)
            make.height.equalTo(CGFloat.app_btnHeight)
            make.centerX.equalToSuperview()
        }
        checkAdBtn.snp.makeConstraints { make in
            make.width.height.centerX.equalTo(buyBtn)
            make.top.equalTo(buyBtn.snp.bottom).offset(12)
            make.bottom.equalTo(-32)
        }
        
        backView.backgroundColor = .white
        
        buyBtn.gradientColors = UIColor.app_themeGradientColors
        buyBtn.addTarget(self, action: #selector(buyAction))
        
        checkAdBtn.addTarget(self, action: #selector(checkAdAction))
        
    }
    private lazy var backView = UIView()
    private lazy var iconImgView: UIImageView = {
        let iconImgView = UIImageView()
        iconImgView.image = UIImage(named: "lock_big_icon")
        iconImgView.contentMode = .scaleAspectFit
        return iconImgView
    }()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangBlod(20)
        titleLabel.text = "更多内容点击解锁".localized
        titleLabel.textColor = UIColor.hex(hexString: "#C9A483")
        return titleLabel
    }()
    private lazy var buyBtn: GradientButton = {
        let buyBtn = GradientButton()
        buyBtn.setTitle("付费解锁".localized)
        buyBtn.setFont(kSetPingFangBlod(15))
        return buyBtn
    }()
    private lazy var checkAdBtn: CustomClickableView = {
        let checkAdBtn = CustomClickableView()
        checkAdBtn.setTitle("看广告，免费解锁".localized)
        checkAdBtn.setFont(kSetPingFangBlod(15))
        checkAdBtn.setTitleColor(UIColor.hex(hexString: "#868D9A"))
        checkAdBtn.layer.cornerRadius = CGFloat.app_btnHeight/2
        checkAdBtn.layer.borderWidth = 1
        checkAdBtn.layer.borderColor = UIColor.hex(hexString: "#252A35").withAlphaComponent(0.15).cgColor
        return checkAdBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension LockScenicAlertView {
    
    func show() {
        
        guard rootView != nil else { return }
        
        UIView.animate(withDuration: CGFloat.app_alertShowInterval-0.1) {
            self.backgroundView.backgroundColor = .app_customAlertBackgroundColor
        }

        DispatchQueue.main.async {
            self.buyBtn.layerCornerRadius(CGFloat.app_btnHeight/2)
            self.backView.layerCornerRadius(12)
        }

        backView.layoutIfNeeded()
    }
    
    private func hide(completion: (() -> Void)? = nil) {
        
        UIView.animate(withDuration: CGFloat.app_alertHidenInterval, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIView.AnimationOptions(rawValue: 0), animations: {
            self.backView.alpha = 0
            self.backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0)
        }) { finished in
            guard finished else { return }
            self.removeAppMiddleAlertView()
        }
        
    }
    func removeAppMiddleAlertView() {
        self.backgroundView.removeFromSuperview()
        self.removeFromSuperview()
    }
        
}




